﻿using Inspire.Classes.Business;
using Inspire.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Inspire.Windows
{
    /// <summary>
    /// Interaction logic for EditCustomFieldType.xaml
    /// </summary>
    public partial class EditCustomFieldType : Window
    {
        /// <summary>
        /// Get a reference to the CustomFieldBusiness class so we can execute some queries.
        /// </summary>
        private CustomFieldBusiness _customFieldBusiness = CustomFieldBusiness.GetCustomFieldBusiness();

        /// <summary>
        /// The Custom Field being edited or created.
        /// </summary>
        private CustomFieldType _editedCustomFieldType = new CustomFieldType();
        public CustomFieldType EditedCustomFieldType
        {
            get
            {
                return this._editedCustomFieldType;
            }
            set
            {
                this._editedCustomFieldType = value;
            }
        }

        /// <summary>
        /// The number of stored records that use this CustomFieldType.
        /// </summary>
        private int _storedRecordCount = 0;
        public int StoredRecordCount
        {
            get
            {
                return this._storedRecordCount;
            }
        }

        /// <summary>
        /// Default constructor.
        /// </summary>
        public EditCustomFieldType(CustomFieldType editedCustomFieldType)
        {
            #region Prepare Fields
            
            // Get an attached copy of the CustomFieldType
            this._editedCustomFieldType = this._customFieldBusiness.GetCustomFieldTypeById(editedCustomFieldType.Id);

            // Get the number of entries of this CustomFieldType
            this._storedRecordCount = this._customFieldBusiness.CountRecordsOfCustomFieldType(this._editedCustomFieldType);

            #endregion

            InitializeComponent();
        }

        /// <summary>
        /// Re-enable the main window when this dialog is closed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closed(object sender, EventArgs e)
        {
            this.Owner.IsEnabled = true;
        }

        /// <summary>
        /// Handle showing the warning message and delete button when the checkbox is selected.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteCustomFieldType_Checked(object sender, RoutedEventArgs e)
        {
            this.btnCancel.Visibility = System.Windows.Visibility.Collapsed;
            this.gridDeleteCustomFieldType.Visibility = System.Windows.Visibility.Visible;
        }

        /// <summary>
        /// Handle hiding the warning message and delete button when the checkbox is deselected.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteCustomFieldType_Unchecked(object sender, RoutedEventArgs e)
        {
            this.gridDeleteCustomFieldType.Visibility = System.Windows.Visibility.Collapsed;
            this.btnCancel.Visibility = System.Windows.Visibility.Visible;
        }

        /// <summary>
        /// Permanently deletes a custom field type and all of its stored records.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteCustomFieldType_Click(object sender, RoutedEventArgs e)
        {
            // Count the number of fields deleted because this CustomFieldType has been deleted
            int customFieldsDeleted = 0;

            // Delete the Custom Field Type
            customFieldsDeleted = this._customFieldBusiness.DeleteCustomFieldType(this._customFieldBusiness.GetCustomFieldTypeById(this._editedCustomFieldType.Id));

            // Close the window since this CustomFieldType no longer exists
            this.Close();
        }

        /// <summary>
        /// Handles the user clicking the Cancel button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
